<?php
/**
 * 广告信息控制器
 *
 * @package     Include  (controller)
 * @author      hary <hary@taomee.com>
 * @version     $ID 2010-3-31 $
 */
class Admin_banner extends Controller
{
	private $result;

	/**
	 * 构造函数
	 */
	function __construct()
	{
		parent::__construct();

		$this->loadModel('ads_banner_model');

		$this->loadClass('Mcache');

		$this->tpl->template_dir = SYS_PATH  . 'template/admin/banner/';

	}

	/**
	 * 默认执行方法
	 *
	 * @return
	 */
	function index()
	{
		$this->getBanners();
	}

	/**
	 * 添加广告信息
	 * @return max
	 */
	function addBanner(){

		$this->tpl->assign("projects",$this->getProjects());
		$this->tpl->assign("zones",$this->getZones());
		$this->tpl->assign("modals",$this->getModals());
		$this->tpl->display("banner.html");
	}

	/**
	 * 编辑信息
	 * @return max
	 */
	function editBanner(){

		$item = $this->in_var['item'];

		$banner = $this->ads_banner_model->getAdsBannerById($item);

		$this->tpl->assign("projects",$this->getProjects());
		$this->tpl->assign("zones",$this->getZones());
		$this->tpl->assign("modals",$this->getModals());
		$this->tpl->assign("banner",$banner);
		$this->tpl->display("banner.html");
	}

	/**
	 * 获取banners
	 * @return max
	 */
	function getBanners(){

		$page = max(intval($this->in_var['page']),1);

		$this->result['banners'] = $this->ads_banner_model->getAdsBannersByKeywords('',$page,5);

		$page_info = $this->ads_banner_model->getPageInfo('/admin'.get_url(strtolower(__CLASS__), __FUNCTION__, 'page={Page}'), 3);

		$this->tpl->assign("projects",$this->getProjects());
		$this->tpl->assign("zones",$this->getZones());
		$this->tpl->assign('page_info',$page_info);
		$this->tpl->assign('result',$this->result);
		$this->tpl->display("banners.html");
	}

	/**
	 * 根据产品获取banners
	 * @return max
	 */
	function getBannersByPro(){

		$id = $this->in_var["item"];

		$this->result['banners'] = $this->ads_banner_model->getAdsBannersByPro($id);

		$page_info = $this->ads_banner_model->getPageInfo(get_url(strtolower(__CLASS__), __FUNCTION__, 'page={Page}'), 3);

		$this->tpl->assign("projects",$this->getProjects());
		$this->tpl->assign("zones",$this->getZones());
		$this->tpl->assign("result",$this->result);
		$this->tpl->assign("page_info",$page_info);
		$this->tpl->display("banners.html");

	}

	/**
	 * 根据广告位获取banners
	 * @return unknown_type
	 */
	function getBannersByZone(){

		$id = intval($this->in_var["item"]);

		$page = max(intval($this->in_var['page']),1);

		$this->result['banners'] = $this->ads_banner_model->getBannersByZone($id);

		$page_info = $this->ads_banner_model->getPageInfo('/admin'.get_url(strtolower(__CLASS__), __FUNCTION__, 'page={Page}'), 3);

		$this->tpl->assign("projects",$this->getProjects());
		$this->tpl->assign("zones",$this->getZones());
		$this->tpl->assign('page_info',$page_info);
		$this->tpl->assign('result',$this->result);
		$this->tpl->display("banners.html");

	}

	/**
	 * 保存banner
	 * @return max
	 */
	function saveBanner(){

		$banner = $this->in_var["banner"];

		if(!empty($banner['b_id']))
		{
			$flag    = $this->ads_banner_model->changeAdsBannerById($banner);
			//清除缓存文件
			$banner_ = $this->ads_banner_model->getAdsBannerById($banner['b_id']);
				
			$this->tpl->template_dir = SYS_PATH  . 'template/web/formwork/';
			$this->tpl->clear_cache($banner_['b_mtype']['mt_formwork_name'],$banner_['b_id']);
				
			//清空memcache缓存
			$this->Mcache->clearCache('banner',$banner_['b_id']);
		}
		else
		{
			$flag = $this->ads_banner_model->addAdsBanner($banner);
		}

		exit(json_encode($flag?array('info'=>$this->sys_conf_var['ads']['customer_msg']['banner']['save_success'],'status'=>true):array('info'=>$this->sys_conf_var['ads']['customer_msg']['project']['save_error'],'status'=>false)));
	}

	/**
	 * 批量删除banner
	 * @return max
	 */
	function delBanners(){

		$ids = $this->in_var['item'];

		$id_ = explode(',',$ids);

		try{
			foreach($id_ as $id)
			{
				$this->ads_banner_model->delAdsBannerById($id);
			}
		}
		catch(Exception $e)
		{
			exit(json_encode(array('info'=>$this->sys_conf_var['ads']['customer_msg']['project']['del_error'],'status'=>false)));
		}

		exit(json_encode(array('info'=>$this->sys_conf_var['ads']['customer_msg']['project']['del_success'],'status'=>true)));
	}

	/**
	 * 按条件查询banner
	 * @return max
	 */
	function searchBanners(){

		$keywords = $this->in_var['keywords'];
		$page     = max($this->in_var['page'],1);

		$this->result['banners'] = $this->ads_banner_model->getAdsBannersByKeywords($keywords,$page,5);

		$page_info = $this->ads_banner_model->getPageInfo('/admin'.get_url(strtolower(__CLASS__), __FUNCTION__, 'page={Page}','keywords='.$this->in_var['keywords']), 3);

		$this->tpl->assign("projects",$this->getProjects());
		$this->tpl->assign("zones",$this->getZones());
		$this->tpl->assign('page_info',$page_info);
		$this->tpl->assign("result",$this->result);
		$this->tpl->display("banners.html");
	}


	/**
	 * 获取projects
	 * @return max
	 */
	function getProjects(){

		$this->loadModel('ads_project_model');
		$projects = $this->Mcache->getCache("ads",'projects');

		if(empty($projects))
		{
			$projects = $this->ads_project_model->getAdsProjectsByKeywords('','','');
			$this->Mcache->setCache('ads','projects',$projects,0,3600);
		}
		else
		{
			$projects = $this->Mcache->getCache('ads','projects');
		}

		return $this->resetArray($projects,'p_id');

	}

	/**
	 * 获取zones
	 * @return max
	 */
	function getZones(){

		$this->loadModel('ads_zone_model');

		$zones = $this->Mcache->getCache("ads",'zones');

		if(empty($zones))
		{
			$zones = $this->ads_zone_model->getAdsZonesByKeywords('','','');
			$this->Mcache->setCache('ads','zones',$zones,0,3600);
		}
		else
		{
			$zones = $this->Mcache->getCache('ads','zones');
		}

		return $this->resetArray($zones,'z_id');
	}

	/**
	 * 获取模本类型
	 * @return max
	 */
	function getModals($id){

		$this->loadModel('ads_modal_model');

		$modals = $this->Mcache->getCache("ads",'modals');

		if(empty($modals))
		{
			$modals = $this->ads_modal_model->getModalsByKeywords('','','');
			$this->Mcache->setCache('ads','modals',$modals,0,3600);
		}
		else
		{
			$modals = $this->Mcache->getCache('ads','modals');
		}

		return $this->resetArray($modals,'mt_id');
	}

	/**
	 * 重置数组
	 * @return max
	 */
	function resetArray($arr,$field){

		$arrs = null;

		foreach($arr as $item)
		{
			$arrs[$item[$field]] = $item;
		}

		return $arrs;
	}

	/**
	 * 发布
	 * @return max
	 */
	function release(){

		$zone = $this->in_var['zone'];

		$isrelease = $this->ads_banner_model->hasRelease($zone);

		$bid = intval($this->in_var['b_id']);

		$unreleased = null;

		if($isrelease)
		{
			$this->cleanFiles($zone);
			
			foreach($isrelease as $item)
			{
				$this->ads_banner_model->changeStatus($item['b_id'],0);
				$unreleased[] = $item['b_id'];
			}
				
		}

		$flag = $this->ads_banner_model->changeStatus($bid,1);

		//清缓存
		$this->Mcache->clearCache('banner',$bid);

		exit(json_encode(!$flag?array('info'=>$this->sys_conf_var['ads']['customer_msg']['banner']['release_error'],'status'=>false):array('info'=>$this->sys_conf_var['ads']['customer_msg']['banner']['release_success'],'status'=>true,'unreleased'=>$unreleased )));
	}

	/**
	 * 清空缓存文件
	 * @return unknown_type
	 */
	private function cleanFiles($key){
		
		$banner = $this->ads_banner_model->getBannerByZone($key);
		
		$tpl_name = $banner['b_mtype']['mt_formwork_name'];
		
		$this->tpl->template_dir = SYS_PATH  . 'template/web/formwork/';
		
		if($this->tpl->isCached($tpl_name, $key))
		{
			$this->tpl->clear_cache($tpl_name, $key);
		}
	}

	/**
	 * 撤销广告
	 * @return max
	 */
	function unRelease(){

		$bid = intval($this->in_var['b_id']);

		$flag = $this->ads_banner_model->changeStatus($bid,0);

		$this->Mcache->clearCache('banner',$bid);

		exit(json_encode(!$flag?array('info'=>$this->sys_conf_var['ads']['customer_msg']['banner']['unrelease_error'],'status'=>false):array('info'=>$this->sys_conf_var['ads']['customer_msg']['banner']['unrelease_success'],'status'=>true )));
	}

	/**
	 * 撤销
	 * @return max
	 */
	function hasRelease(){

		$zone = $this->in_var['zone'];

		$isrelease = $this->ads_banner_model->hasRelease($zone);

		echo $isrelease?1:0;
	}

}